﻿@page "/fetchdata/todoitem/details/{Id:int}"

@using MaBlaApp.Data
@inject TodoService MyTodoService
@inject IJSRuntime JSRuntime

<ul class="list-group">
    <li class="list-group-item list-group-item-primary d-flex justify-content-between">
        <strong class="align-self-center">任务详细</strong>
        <button class="btn btn-primary btn-sm" @onclick=Back>返回</button>
    </li>
</ul>

@if (todoItem is null)
{
    <p><em>任务不存在...</em></p>
}
else
{
    <ul class="list-group" style="overflow:auto">
        @foreach (var item in items)
        {
            <li class="list-group-item d-flex justify-content-between mb-1">
                <strong>@item.Key</strong>
                <small>@item.Value</small>
            </li>
        }

        <li class="list-group-item d-grid">
            <a href=@editUrl class="btn btn-warning btn-sm btn-block">修改</a>
        </li>
    </ul>
}

@code {
    [Parameter]
    public int Id { get; set; }

    private TodoItem todoItem;

    private Dictionary<string, string> items = new Dictionary<string, string>();

    private Dictionary<TodoPriority, string> TodoPriorityName = new Dictionary<TodoPriority, string>();

    private string editUrl;

    protected override void OnInitialized()
    {
        todoItem = MyTodoService.TodoItems.FirstOrDefault(x => x.Id == Id);

        if (todoItem is null)
            return;

        TodoPriorityName[TodoPriority.Low] = "低";
        TodoPriorityName[TodoPriority.Middle] = "中";
        TodoPriorityName[TodoPriority.High] = "高";

        items[nameof(todoItem.Id)] = $"{todoItem.Id}";
        items[nameof(todoItem.Title)] = $"{todoItem.Title}";
        items[nameof(todoItem.Description)] = $"{todoItem.Description}";
        items[nameof(todoItem.CreatedDate)] = $"{todoItem.CreatedDate:d}";
        items[nameof(todoItem.Priority)] = TodoPriorityName[todoItem.Priority];
        items[nameof(todoItem.IsFinished)] = $"{todoItem.IsFinished}";

        editUrl = $"fetchdata/todoitem/edit/{todoItem.Id}";
    }

    private async void Back()
    {
        await JSRuntime.InvokeVoidAsync("window.history.back");
    }

}
